<?php
/**
 * 投保记录
 * @Author    紫梦
 * @DateTime  2018-01-10
 * @copyright 响响租车
 * @version   1.0
 */
namespace app\wxapp\controller;
use \think\Request;
use app\common\model\Admin as AdminModel;
use app\common\model\Roles;
use app\common\model\Agency;

class Admin extends Base
{
	
	/**
     * 记录列表
     */
    public function index($page=1,$length=10,$seach='')
    {
        $model= new AdminModel;
        if(empty($seach)){
            $where = '1=1';
        }else{
            $where = [
                'user_name|nick_name|email'=>['like','%'.$seach.'%']
                ];
        }
        try {           
            $data = $model->order('id', 'desc')
                        ->where($where)
                        ->field('id,user_name,nick_name,email,last_login,status')
                        ->paginate($length,false,[
                                'type'     => 'bootstrap',
                                'var_page' => 'page',
                                'page'=>$page ,
                            ]);             
        } catch (\Exception $e) {
            
            return [
                'code'=>2,
                'msg'=>$e->getMessage(),
                ];
        }
        return $data;
    }

    
    /**
     * 记录详情
     */
    public function detail($id = 1)
    {
        try {           
            $model = AdminModel::get($id);                      
            $model->roles;
        } catch (\Exception $e) {
            
            return [
                'code'=>2,
                'msg'=>$e->getMessage(),
                ];
        }
        return $model;
    }

    /**
     * 添加记录
     */
    public function add()
    {
        $request = Request::instance();
        if($request->isPost()){
            $data = $request->post();
            $data['password'] = password_hash($data['password'],PASSWORD_DEFAULT);

            $model = new AdminModel();
            
            try{
               
                $model->allowField(true)->save($data);

            }catch(\Exception $e){
                return [
                    'code'=>2,
                    'msg'=>$e->getMessage(),
                ];
            }

            return [
                'code'=>1,
                'msg'=>'添加成功'
                ];
        }else{
            $data['roles'] = Roles::all();
            $data['agency'] = Agency::all();

            return $data;

        }           
    }

    /**
     * 编辑记录
     */
    public function edit($id)
    {
        $request = Request::instance();

        if($request->isPost()){
            $data = $request->post();
            $model = new AdminModel();
            try{
               
                $model->allowField(true)->isUpdate(true)->save($data);

            }catch(\Exception $e){
                return [
                    'code'=>2,
                    'msg'=>$e->getMessage(),
                ];
            }

            return [
                'code'=>1,
                'msg'=>'更新成功'
                ];
        }else{
            $data['roles'] = Roles::all();
            $data['agency'] = Agency::all();
            $data['admin'] = AdminModel::get($id);

            return $data;
        }
    }

    /**
     * 删除记录
     */
    public function delete($id)
    {
        
        try {
            AdminModel::destroy($id);
            
        } catch (\Exception $e) {
            return [
                'code'=>2,
                'msg'=>$e->getMessage()
                ];
            
        }
        
        return [
            'code'=>1,
            'msg'=>'删除成功'
            ];
    }


}